//
//  SingleSelectedDialog.m
//  HappyStore
//
//  Created by 点点充 on 16/1/22.
//  Copyright © 2016年 LLJ. All rights reserved.
//

#import "SingleSelectedDialog.h"

@interface SingleSelectedDialog ()<UIPickerViewDataSource,UIPickerViewDelegate>
{
    UIPickerView *mainPick;
    UIButton *buttonOK;
    
    NSArray *aryData;
    
    NSInteger selectedRow;
}
@end

@implementation SingleSelectedDialog

+ (SingleSelectedDialog *)sharedSelectedAddress
{
    static SingleSelectedDialog *_sharedService = nil;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedService = [[SingleSelectedDialog alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _sharedService.backgroundColor = [[UIColor colorWithWhite:0.6 alpha:1.0f] colorWithAlphaComponent:0.7];
    });
    return _sharedService;
}

#pragma mark - 选择地区
- (void)btnClickAddressWithData:(NSArray *)arydata
{
    aryData = arydata;
    self.hidden = NO;
    //加入手势来取消键盘
    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
                                             initWithTarget:self action:@selector(dismissView)];
    tapRecognizer.cancelsTouchesInView = NO;
    [self addGestureRecognizer:tapRecognizer];
    
    selectedRow = 0;
    mainPick = [[UIPickerView alloc] initWithFrame: CGRectMake(0, [UIScreen mainScreen].bounds.size.height-240 - 50, 320, 240)];
    mainPick.dataSource = self;
    mainPick.delegate = self;
    mainPick.showsSelectionIndicator = YES;
    [mainPick selectRow: 0 inComponent: 0 animated: YES];
    mainPick.backgroundColor = [UIColor whiteColor];
    [self addSubview: mainPick];
    
    
    buttonOK = [UIButton buttonWithType: UIButtonTypeCustom];
    [buttonOK setTitle: @"确  定" forState: UIControlStateNormal];
//    [buttonOK setFrame: CGRectMake(320-60, mainPick.frame.origin.y - 40, 50, 30)];
    [buttonOK setFrame:CGRectMake(0, mainPick.frame.origin.y + mainPick.frame.size.height, screenW, 50)];
    [buttonOK setTintColor: [UIColor grayColor]];
//    buttonOK.layer.cornerRadius = 5;
    [buttonOK setBackgroundColor:[UIColor colorWithRed:32/255.0f green:165/255.0f  blue:240/255.0f alpha:1.0f]];
    [buttonOK addTarget: self action: @selector(buttobClicked:) forControlEvents: UIControlEventTouchUpInside];
//    buttonOK.titleLabel.font = [UIFont systemFontOfSize:15.f];
    buttonOK.titleLabel.font = [UIFont systemFontOfSize:20.0f];
    [self addSubview: buttonOK];
    
    [[[[UIApplication sharedApplication] windows] lastObject] addSubview:self];
    
}

- (void)buttobClicked:(UIButton *)sender
{
    [self.delegatee seceltedRow:selectedRow];
}

#pragma mark- Picker Data Source Methods

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return aryData.count;
}


#pragma mark- Picker Delegate Methods

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return aryData[row];
}


- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    selectedRow = row;
    
}


//- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
//{
//    if (component == PROVINCE_COMPONENT) {
//        return 80;
//    }
//    else if (component == CITY_COMPONENT) {
//        return 100;
//    }
//    else {
//        return 115;
//    }
//}



- (void)dismissView
{
    self.hidden = YES;
}




@end
